Conditions | 12 |
Total Lines | 66 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like index.ts ➔ template often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import * as fs from "fs"; |
||
33 | |||
34 | export /** |
||
35 | * |
||
36 | * @param title |
||
37 | * @param content |
||
38 | * @param param |
||
39 | */ |
||
40 | function template( |
||
41 | title: |
||
42 | | string |
||
43 | | ((arg0: http.IncomingMessage, arg1: http.ServerResponse) => string), |
||
44 | content?: [http.IncomingMessage, http.ServerResponse] | string, |
||
45 | param?: [http.IncomingMessage, http.ServerResponse] |
||
46 | ) { |
||
47 | var status = 200; |
||
48 | |||
49 | if (typeof title == "string") { |
||
50 | var template = fs |
||
51 | .readFileSync("./assets/node/template/mdb.html") |
||
52 | .toString(); |
||
53 | |||
54 | var body = `./assets/node/views/${content}.html`; |
||
55 | if (fs.existsSync(body)) { |
||
56 | template = template.replace( |
||
57 | /\%content\%/gm, |
||
58 | fs.readFileSync(body).toString() |
||
59 | ); |
||
60 | } else { |
||
61 | console.warn("body not found"); |
||
62 | status = 404; |
||
63 | template = template.replace( |
||
64 | /\%content\%/gm, |
||
65 | readFile(asset("./assets/node/views/404.html")) |
||
66 | ); |
||
67 | } |
||
68 | var script = `./assets/node/views/${content}.js`; |
||
69 | if (fs.existsSync(script)) { |
||
70 | template = template.replace( |
||
71 | /\%script\%/gm, |
||
72 | `<script src="${config.app.protocol}://${ |
||
73 | config.app.domain |
||
74 | }/assets/node/views/${content}.js${ |
||
75 | config.app.environtment == "development" |
||
76 | ? "?cache=" + new Date().getTime() |
||
77 | : "" |
||
78 | }"></script>` |
||
79 | ); |
||
80 | } else { |
||
81 | console.log("script not found"); |
||
82 | } |
||
83 | param[1].writeHead(status, { "Content-Type": "text/html" }); |
||
84 | |||
85 | template = template.replace(/\%protocol\%/gm, config.app.protocol); |
||
86 | template = template.replace(/\%domain\%/gm, config.app.domain); |
||
87 | template = template.replace(/\%title\%/gm, title); |
||
88 | template = template.replace(/\%config\%/gm, JSON.stringify(config)); |
||
89 | return template; |
||
90 | } else if (typeof title == "function") { |
||
91 | if (Array.isArray(content)) { |
||
92 | content[1].writeHead(status, { "Content-Type": "application/json" }); |
||
93 | return title(content[0], content[1]); |
||
94 | } else if (Array.isArray(param)) { |
||
95 | param[1].writeHead(status, { "Content-Type": "application/json" }); |
||
96 | return title(param[0], param[1]); |
||
97 | } else { |
||
98 | return "Can't process anything :("; |
||
99 | } |
||
183 |